<% # frozen_string_literal: true %>

<%
  with_inherit ||= false
  assignment = assignable.user_assignments.find_by(user_id: user.id, team: current_team)
  item_id = dom_id(user, :assignment_member)
%>

<% if assignment.present? %>
  <%= form_with(model: assignment, url: update_path, method: :put, html: { class: 'member-item', id: item_id, data: { remote: true, action: 'replace-form autosave-form', object_type: :assignment_member } }) do |f| %>
    <%= f.hidden_field :user_id, value: f.object.user.id %>
    <div class="user-assignment-info">
      <div class="global-avatar-container">
        <%= image_tag avatar_path(user, :icon_small), title: current_assignee_name(user), class: 'img-circle pull-left' %>
      </div>
      <div>
        <%= current_assignee_name(user) %>
        <br>
        <small class="text-muted"><%= user_assignment_resource_role_name(user, assignable) %></small>
      </div>
    </div>
    <div class="user-assignment-controls">
      <% unless with_inherit && current_user == user %>
        <div class="dropdown pull-right">
          <button class="btn btn-light btn-role-select" type="button" id="userAccess_<%= user.id %>" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
            <%= f.object.user_role.name %>
            <span class="caret"></span>
          </button>
          <%= f.hidden_field :user_role_id, value: f.object.user_role.id %>
          <ul class="dropdown-menu dropdown-menu-right user-assignment-dropdown" aria-labelledby="userAccess_<%= user.id %>">
            <% user_roles_collection(assignable, with_inherit: with_inherit).each do |role| %>
              <li>
                <a href="#" data-turbolinks="false" class="user-role-selector" title="<%= role[2] %>" data-role-id="<%= role[1] %>">
                  <%= role[0] %>
                </a>
              </li>
            <% end %>
            <% if defined?(delete_path) && !assignment.last_with_permission?(ProjectPermissions::USERS_MANAGE, assigned: :manually) %>
              <li role="separator" class="divider" data-hook="support-dropdown-separator"></li>
              <li>
                <%= link_to delete_path, remote: true, method: :delete, data: { action: 'remote-destroy', target: "##{item_id}" } do %>
                  <%= t('access_permissions.remove_access') %>
                <% end %>
              </li>
            <% end %>
          </ul>
        </div>
      <% else %>
        <button class="btn btn-light btn-role-select disabled" type="button">
          <%= f.object.user_role.name %>
        </button>
      <% end %>
    </div>
  <% end %>
<% end %>
